package tuan.filter;

import java.util.*;

import org.w3c.dom.*;

public class NodeListFilterChain {

	List<NodeFilter> filterList = new ArrayList<NodeFilter>();
	List<Node> nodeList = new ArrayList<Node>();

	public List<Node> filter(List<Node> nodeList) throws Exception {
		if (nodeList == null) {
			throw new Exception("null arguments");
		}
		this.nodeList.clear();
		Iterator<Node> iter = nodeList.iterator();
		while (iter.hasNext()) {
			Node node = iter.next();
			boolean isvalid = true;
			for (NodeFilter filter : filterList) {
				if (!filter.isValid(node)) {
					isvalid = false;
					break;
				}
			}
			if (isvalid) {
				this.nodeList.add(node);
			}
		}
		return this.nodeList;
	}

	public void addFilter(NodeFilter filter) throws Exception {
		if (filter == null) {
			throw new Exception("null arguments");
		}
		filterList.add(filter);
	}

}
